<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 40px;
            background-color: #f9f9f9;
        }

        h2 {
            text-align: center;
        }

        form {
            max-width: 500px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }

        label {
            display: block;
            margin-top: 15px;
            font-weight: bold;
        }

        input[type="text"],
        input[type="number"],
        input[type="date"] {
            width: 100%;
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }

        #message {
            margin-top: 20px;
            text-align: center;
            font-weight: bold;
        }
    </style>
</head>
<body>

<h2>添加新商品</h2>

<form id="addGoodForm">
    <div>
        <label for="name">商品名称：</label>
        <input type="text" id="name" name="name" required />
    </div>

    <div>
        <label for="price">商品价格：</label>
        <input type="number" id="price" name="price" step="0.01" required />
    </div>

    <div>
        <label for="stock">库存数量：</label>
        <input type="number" id="stock" name="stock" required />
    </div>

    <div>
        <label for="date">上架时间：</label>
        <input type="date" id="date" name="date" required />
    </div>

    <button type="submit">提交</button>
</form>

<div id="message"></div>

<script>
    document.getElementById('addGoodForm').addEventListener('submit', function(event) {
        event.preventDefault(); // 阻止表单默认提交行为

        const name = document.getElementById('name').value.trim();
        const price = parseFloat(document.getElementById('price').value);
        const stock = parseInt(document.getElementById('stock').value);
        const date = document.getElementById('date').value;

        if (!name || isNaN(price) || isNaN(stock) || !date) {
            document.getElementById('message').innerText = '请填写完整的信息';
            return;
        }
        axios.post('/goods/add', {
            name: name,
            price: price,
            stock: stock,
            date: date
        })
            .then(function(response) {
                document.getElementById('message').innerText = '添加成功！';

                // 添加成功后跳转到商品列表页
                window.location.href = 'products.html';
            })
            .catch(function(error) {
                let errorMsg = '添加失败，请重试';
                if (error.response && error.response.data) {
                    errorMsg = error.response.data;
                } else if (error.message) {
                    errorMsg = error.message;
                }
                document.getElementById('message').innerText = errorMsg;
            });
    });
</script>

</body>
</html>
